package com.hxd.otc.module.my.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.CountryCodeEnum;
import com.hxd.otc.common.enums.IdCardStatusEnum;
import com.hxd.otc.module.my.contract.RealNameStep2Contract;
import com.hxd.otc.module.my.presenter.RealNameStep2Presenter;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class RealNameStep2Activity extends BaseActivity<RealNameStep2Presenter> implements RealNameStep2Contract.IView {

    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.rb_male)
    RadioButton mRbMale;
    @BindView(R.id.rb_woman)
    RadioButton mRbWoman;
    @BindView(R.id.rg_gender)
    RadioGroup mRgGender;
    @BindView(R.id.et_id_car_no)
    EditText mEtIdCarNo;
    @BindView(R.id.et_age)
    EditText mEtAge;
    @BindView(R.id.tv_submitted)
    TextView mTvSubmitted;
    @BindView(R.id.tv_next)
    TextView mTvNext;

    public static final String EXTRA_REAL_NAME_TYPE = "real_name_type";
    public static final String EXTRA_REAL_NAME_INFO = "real_name_info";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_real_name_step_2;
    }

    @Override
    protected RealNameStep2Presenter createPresenter() {
        return new RealNameStep2Presenter(this);
    }

    @Override
    protected void initView() {
        Bundle bundle = getBundle();

        CountryCodeEnum realNameType = (CountryCodeEnum) bundle.getSerializable(EXTRA_REAL_NAME_TYPE);
        mPresenter.setRealNameType(realNameType);
        mTvSubmitted.setVisibility(CountryCodeEnum.CHINA.equals(realNameType) ? View.VISIBLE : View.GONE);
        mTvNext.setText(CountryCodeEnum.CHINA.equals(realNameType) ? R.string.continue_advanced_certification : R.string.next);
    }

    @OnClick({R.id.tv_submitted, R.id.tv_next})
    public void onViewClicked(View view) {
        String name = mEtName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            showToast(mEtName.getHint().toString());
            mEtName.requestFocus();
            return;
        }
        String address = mEtAddress.getText().toString().trim();
        if (TextUtils.isEmpty(address)) {
            showToast(mEtAddress.getHint().toString());
            mEtAddress.requestFocus();
            return;
        }
        String idCardNo = mEtIdCarNo.getText().toString().trim();
        if (TextUtils.isEmpty(idCardNo)) {
            showToast(mEtIdCarNo.getHint().toString());
            mEtIdCarNo.requestFocus();
            return;
        }
        String age = mEtAge.getText().toString().trim();
        if (TextUtils.isEmpty(age)) {
            showToast(mEtAge.getHint().toString());
            mEtAge.requestFocus();
            return;
        }
        boolean isMale = mRbMale.isChecked();


        switch (view.getId()) {
            case R.id.tv_submitted://直接进行LV1认证
                mPresenter.realNameVerifyOfV1(idCardNo, age, isMale ? 1 : 2, name, address);
                break;
            case R.id.tv_next://直接进行高级认证
                Bundle bundle = new Bundle();

                HashMap<String, Object> realNameInfo = new HashMap<>();
                realNameInfo.put("idCardNo", idCardNo);
                realNameInfo.put("age", age);
                realNameInfo.put("isMale", isMale ? 1 : 2);
                realNameInfo.put("name", name);
                realNameInfo.put("address", address);

                bundle.putSerializable(EXTRA_REAL_NAME_INFO, realNameInfo);
                bundle.putSerializable(EXTRA_REAL_NAME_TYPE, mPresenter.getRealNameType());
                $startActivity(RealNameStep1Activity.class, bundle, true);
                break;
        }
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected void initData() {
    }

    @Override
    public void CommitRealNameInfoSuccess() {
        UserHelper.updateCardStatus(IdCardStatusEnum.V1_Auditing);
        finish();
    }
}
